﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using ChanExt;
using C.Unite;
using C.Entities;
using MySoft.Data;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class SysUserSetPer : CommonPageBase
    {
        List<Sys_Modules> items = TbsDbSession.Default.From<Sys_Modules>().OrderBy(Sys_Modules._.Orderid.Asc).ToList();
        List<Sys_PerInfo> tplist = new List<Sys_PerInfo>();// TbsDbSession.Default.From<GS_PerInfo>().Where(GS_PerInfo._.Userid == int.Parse(HttpContext.Current.Request["uid"]) && GS_PerInfo._.Pertype == int.Parse(HttpContext.Current.Request["mtype"])).ToList();
        List<Sys_ModToPer> modtoperlist = TbsDbSession.Default.From<Sys_ModToPer>().ToList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tplist = DBCaChe.GetPerList().FindAll(delegate(Sys_PerInfo tpi) { return tpi.Userid == int.Parse(HttpContext.Current.Request["uid"]) && tpi.Pertype == int.Parse(HttpContext.Current.Request["mtype"]); });
                LoadData();
            }
        }

        protected void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();
            List<Sys_Modules> tmlist = items.FindAll(delegate(Sys_Modules tmo) { return tmo.Topmid == 0; });
            foreach (Sys_Modules row in tmlist)
            {
                TreeNode node = new TreeNode();
                node.Text = row.ModeuleName;
                node.NodeID = row.Mid.ToString() + "-" + "0-" + row.Topmid.Value.ToString() + "-View-" + row.Mid;
                node.ToolTip = row.Memo;
                node.Expanded = false;
                node.EnableCheckBox = true;
                node.EnablePostBack = true;
                node.AutoPostBack = true;
                node.Checked = GetIsHaveCheck(row.Mid);
                MgrTree.Nodes.Add(node);
                ResolveSubTree(row.Mid, node);
            }
        }
        private void ResolveSubTree(int id, TreeNode treeNode)
        {
            List<Sys_Modules> tmlist = items.FindAll(delegate(Sys_Modules tmo) { return tmo.Topmid == id; });
            if (tmlist.Count > 0)
            {
                treeNode.Expanded = false;
                foreach (Sys_Modules row in tmlist)
                {
                    TreeNode node = new TreeNode();
                    node.Text = row.ModeuleName;
                    node.NodeID = row.Mid.ToString() + "-" + "0-" + row.Topmid.Value.ToString() + "-View-" + row.Mid;
                    node.ToolTip = row.Memo;
                    node.EnablePostBack = true;
                    node.EnableCheckBox = true;
                    node.AutoPostBack = true;
                    node.Checked = GetIsHaveCheck(row.Mid);
                    node.Expanded = true;
                    treeNode.Nodes.Add(node);
                    List<Sys_ModToPer> list = modtoperlist.FindAll(delegate(Sys_ModToPer tmp) { return tmp.Mid == row.Mid; });
                    foreach (Sys_ModToPer item in list)
                    {
                        Sys_ModPermis tmp = TbsDbSession.Default.From<Sys_ModPermis>().Where(Sys_ModPermis._.Perid == item.Perid).ToFirst();
                        if (null != tmp)
                        {
                            TreeNode tn = new TreeNode();
                            tn.Text = tmp.Pername;
                            tn.NodeID = item.Mpid.ToString() + "-" + "1-0-" + tmp.Keyword + "-" + item.Mid;
                            tn.ToolTip = tmp.Memo;
                            tn.EnablePostBack = true;
                            tn.EnableCheckBox = true;
                            tn.Checked = GetIsHaveCheck1(item.Mpid);
                            tn.AutoPostBack = true;
                            node.Nodes.Add(tn);
                        }
                    }
                    ResolveSubTree(row.Mid, node);
                }
            }
        }


        protected void Dgnodes(string topid)
        {
            Sys_Modules topmid = items.Find(delegate(Sys_Modules tms1) { return tms1.Mid == int.Parse(topid); });
            try
            {
                TreeNode tns = MgrTree.FindNode(topmid.Mid + "-" + "0-" + topmid.Topmid.Value + "-View-" + topmid.Mid);
                tns.Checked = true;
                Dgnodes(topmid.Topmid.Value.ToString());

            }
            catch (Exception ef1)
            { }
        }


        protected void Tree1_NodeCheck(object sender, ChanExt.TreeCheckEventArgs e)
        {
            List<string> nodeidlist = StringHelper.GetStrArray(e.NodeID, '-');
            if (nodeidlist[1] == "1")
            {
                string topmid = items.Find(delegate(Sys_Modules tms1) { return tms1.Mid == int.Parse(nodeidlist[4].ToString()); }).Topmid.ToString();
                try
                {
                    TreeNode tns = MgrTree.FindNode(nodeidlist[4] + "-" + "0-" + topmid + "-View-" + nodeidlist[4]);
                    tns.Checked = true;
                    Dgnodes(topmid);

                }
                catch (Exception ef)
                { }


            }
            else
            {
                try
                {
                    TreeNode tns = MgrTree.FindNode(nodeidlist[2] + "-" + "0-0-View-" + nodeidlist[2]);
                    tns.Checked = true;
                    Dgnodes(nodeidlist[2]);
                }
                catch (Exception ef1)
                { }
            }
            if (!e.Node.Leaf)
            {
                CheckTreeNode(e.Node.Nodes, e.Checked);
            }

        }

        private void CheckTreeNode(TreeNodeCollection nodes, bool isChecked)
        {

            foreach (TreeNode node in nodes)
            {
                node.Checked = isChecked;
                if (!node.Leaf)
                {
                    CheckTreeNode(node.Nodes, isChecked);
                }
            }
        }

        /// <summary>
        ///取得是否有权限 
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public bool GetIsHaveCheck(int pid)
        {
            bool b = false;

            Sys_PerInfo tp = tplist.Find(delegate(Sys_PerInfo tpinfo) { return tpinfo.Mid == pid; });// TbsDbSession.Default.From<GS_PerInfo>().Where(GS_PerInfo._.Mid == pid && GS_PerInfo._.Userid == int.Parse(Request["uid"]) && GS_PerInfo._.Pertype == int.Parse(Request["mtype"])).ToFirst();
            if (null != tp)
            {
                b = true;
            }
            return b;
        }
        /// <summary>
        ///取得是否有权限 
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public bool GetIsHaveCheck1(int pid)
        {
            bool b = false;
            Sys_PerInfo tp = tplist.Find(delegate(Sys_PerInfo tpinfo) { return tpinfo.Perid == pid && tpinfo.Pmtype == 1; });
            if (null != tp)
            {
                b = true;
            }
            return b;
        }


        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            TbsDbSession.Default.Delete<Sys_PerInfo>(Sys_PerInfo._.Userid == int.Parse(Request["uid"]) && Sys_PerInfo._.Pertype == int.Parse(Request["mtype"]));
            TreeNode[] nodes = MgrTree.GetCheckedNodes();
            if (nodes.Length > 0)
            {
                using (MySoft.Data.DbBatch batch = TbsDbSession.Default.BeginBatchConnection(10))
                {
                    foreach (TreeNode node in nodes)
                    {
                        List<string> nodeidlist = StringHelper.GetStrArray(node.NodeID, '-');
                        Sys_PerInfo tp = new Sys_PerInfo();
                        tp.DeAttach();
                        tp.Pertype = int.Parse(Request["mtype"]);
                        tp.Perid = int.Parse(nodeidlist[0]);
                        tp.Pmtype = int.Parse(nodeidlist[1]);
                        tp.TopMid = int.Parse(nodeidlist[2]);
                        tp.Userid = int.Parse(Request["uid"]);
                        tp.Pername = nodeidlist[3];
                        tp.Mid = int.Parse(nodeidlist[4]);
                        batch.Insert<Sys_PerInfo>(tp);
                    }
                }
            }
            DBCaChe.ReLoadPerList();
            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
        }
    }
}